package simpleWave;

import java.util.ArrayList;

import data.Wave;
import data.Wave.Direction;


public class Verifier {
	
	public static ArrayList<Wave> verify(ArrayList<String> lines) {
		
		ArrayList<Wave> waves = new ArrayList<Wave>();
		
		//for (int lineNum=0;lineNum<lines.size();lineNum++) {
		int lineNum = lines.size() - 1;
		
			for (int personNum=0;personNum<lines.get(lineNum).length();personNum++) {
				
				if (lines.get(lineNum).charAt(personNum) == 'N') {
					//&& !states.get(i).get(j).isVerifiedWave()
					
					//System.out.println("i: " + line + " j:" + person);
					Wave leftWave = detectLeftWave(lineNum, personNum, lines);
					if (leftWave.numPersons > 2) {
						if (!hasDuplicates(waves, leftWave)) {
							waves.add(leftWave);
						}
					}
					
					Wave rightWave = detectRightWave(lineNum, personNum, lines);
					if (rightWave.numPersons > 2) {
						if (!hasDuplicates(waves, rightWave)) {
							waves.add(rightWave);
						}
					}
				}
			}
		//}
		
		return waves;
		
	}
	
	private static boolean hasDuplicates(ArrayList<Wave> waves, Wave newWave) {
		for (Wave wave : waves) {
			if (wave.endState == newWave.endState &&
					wave.endPosition == newWave.endPosition) {
				return true;
			}
		}
		return false;
	}

	private static Wave detectLeftWave(int lineNum, int personNum, ArrayList<String> lines) {
		
		Wave wave = new Wave();
		wave.direction = Direction.LEFT;
		wave.endState = lineNum ;
		wave.endPosition = personNum;
		int i = 0;
		while(true) {
			
			char person;
			try {
				person = lines.get(lineNum - i).charAt(personNum - i);
			} catch (IndexOutOfBoundsException e) {
				//System.out.println("index state:" + (statePosition + i) + " person:" + (personPosition - i));
				wave.startState = lineNum - (i-1);
				wave.startPosition = personNum - (i-1);
				break;
			}
			
			// Detect wave to the left
			if (person == 'N') {
				// We got wave to the left
				wave.numPersons++;
				
				//System.out.println("standing");
				
			} else {
				wave.startState = lineNum - (i-1);
				wave.startPosition = personNum - (i-1);
				//System.out.println(wave);
				break;
			}
			
			i++;
		}
		
		return wave;
	}
	
	
	private static Wave detectRightWave(int lineNum, int personNum, ArrayList<String> lines) {
		
		Wave wave = new Wave();
		wave.direction = Direction.RIGHT;
		wave.endState = lineNum;
		wave.endPosition = personNum;
		int i = 0;
		while(true) {
			
			char person;
			try {
				person = lines.get(lineNum - i).charAt(personNum + i);
			} catch (IndexOutOfBoundsException e) {
				//System.out.println("index state:" + (statePosition + i) + " person:" + (personPosition - i));
				wave.startState = lineNum - (i-1);
				wave.startPosition = personNum + (i-1);
				break;
			}
			
			// Detect wave to the right
			if (person == 'N') {
				// We got wave to the right
				wave.numPersons++;
				
				//System.out.print("  standing  ");
				
			} else {
				wave.startState = lineNum - (i-1);
				wave.startPosition = personNum + (i-1);
				//System.out.println(wave);
				break;
			}
			
			i++;
		}
		
		return wave;
	}
	
	/*private static Wave detectRightWave(int statePosition, int personPosition, 
			ArrayList<ArrayList<Person>> states) {
		
		Wave wave = new Wave();
		wave.startState = statePosition;
		wave.startPosition = personPosition;
		int i = 0;
		while(true) {
			
			//System.out.println("inside detect right wave");
			Person person;
			try {
				person = states.get(statePosition + i).get(personPosition + i);
			} catch (IndexOutOfBoundsException e) {
				break;
			}
			
			// Detect wave to the left
			if (person.getPersonState() == PersonState.STANDING) {
				// We got wave to the left
				person.setVerifiedWave(true);
				wave.numPersons++;
			} else {
				wave.endPosition = statePosition + (i-1);
				wave.endState = personPosition + (i-1);
				break;
			}
			i++;
		}
		
		return wave;
	}*/

}
